package com.gears_software.sxdc.dccx.ui.view;

import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IUsualAddressFinder;
import com.gears_software.sxdc.dccx.data.model.AddressList;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.ui.activity.UsualAddressActivity;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.HashMap;
import java.util.List;

/**
 * Created by SunMuran on 2017/8/11.
 */
@EViewGroup(R.layout.item_view_collect)
public class CollectListItemView extends LinearLayout {
    @App
    DCCXApplication app;
    @ViewById
    TextView tv_shoucang_big_address;
    @ViewById
    TextView tv_shoucang_small_address;
    @ViewById
    TextView tv_delete_icon;
    @RestService
    IUsualAddressFinder finder;
    Context context;
    AddressList list;
    int  position;

    public CollectListItemView(Context context) {
        super(context);
        this.context = context;
    }

    public void bind(AddressList list,int position) {
        this.list = list;
        this.position = position;
        tv_delete_icon.setTypeface(app.iconfont);
        tv_shoucang_big_address.setText(list.getDetailedaddress());
        tv_shoucang_small_address.setText(list.getDulladdress());
    }

    @Click
    void tv_delete_icon(){
        deleteAddress();
    }

    @Background
    public void deleteAddress(){
        ResultModel<String> result = finder.deleteAddress(list.getId());
        showResult(result);
    }
    @UiThread
    void showResult(ResultModel<String> result){
        if (result != null) {
            if (result.getErrno().equals("0")) {
                TastyToast.makeText(context,"删除地址成功", app.SHORT, app.SUCCESS);
                ((UsualAddressActivity)context).getAddressList();
            }else{
                TastyToast.makeText(context,"删除地址失败", app.SHORT, app.ERROR);
            }
        }
    }

}
